package qq.android.wan.qq;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by mac on 2016/3/8.
 */
public class Tab1AddHistoryRecever extends BroadcastReceiver {
    private ItemLvHistoryAdapter adapter;
    private ArrayList<FriendBean> friends;

    public Tab1AddHistoryRecever(ItemLvHistoryAdapter adapter, ArrayList<FriendBean> friends) {
        this.adapter = adapter;
        this.friends = friends;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String qqId=intent.getStringExtra("qqId");
        String nick=intent.getStringExtra("nick");
        String lastMessage=intent.getStringExtra("lastMessage");
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastTime = formatter.format(date);
        ArrayList<String> tmp=new ArrayList<String>();
        for(FriendBean tmp1:friends){
            tmp.add(tmp1.getNum());
        }
        if(tmp.contains(qqId)){

            for (int i=0;i<friends.size();i++){
                if(friends.get(i).getNum().equals(qqId)){
                    //修改
                    FriendBean friendBean=new FriendBean();
                    friendBean.setLastTime(lastTime);
                    friendBean.setLastMessage(lastMessage);
                    friendBean.setNick(nick);
                    friendBean.setNum(qqId);
                    friends.remove(i);
                    friends.add(0,friendBean);
                    break;
                }
            }
        }else{
            //插入
            FriendBean friendBean=new FriendBean();
            friendBean.setLastMessage(lastMessage);
            friendBean.setNum(qqId);
            friendBean.setNick(nick);
            friendBean.setLastTime(lastTime);
            friends.add(0, friendBean);
        }
        adapter.notifyDataSetChanged();
    }
}
